﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using Microsoft.Reporting.WinForms;
using Infragistics.Windows.Ribbon;

using MVPlan.Engine;
using MVPlan.Engine.Dive;
using MVPlan.Engine.Gases;
using MVPlan.Engine.Segment;
using MVPlan.Reporting;

namespace OneStop.UI
{
    /// <summary>
    /// Interaction logic for TableView.xaml
    /// </summary>
    public partial class TableView : XamRibbonWindow
    {
        public Profile Profile { get; set; }

        ReportViewer rv = new ReportViewer();

        public TableView()
        {
            InitializeComponent();


            windowsFormsHost1.Child = rv;
        }

        public void RenderReport()
        {
            this.Profile.Reset();

            switch ((ProfileStatus)this.Profile.DoDive())
            {
                case ProfileStatus.Success:

                    Assembly a = Assembly.GetAssembly(typeof(SegmentDataSet));

                    using (Stream r = a.GetManifestResourceStream("MVPlan.Reporting.SegmentReport.rdlc"))
                    {
                        rv.LocalReport.LoadReportDefinition(r);

                        ReportDataSource d = new ReportDataSource("SegmentDataSet_DiveSegment", SegmentReportManager.BuildDataSet(this.Profile).DiveSegment);
                        ReportDataSource d1 = new ReportDataSource("SegmentDataSet_DiveInfo", SegmentReportManager.BuildDataSet(this.Profile).DiveInfo);
                        ReportDataSource d2 = new ReportDataSource("SegmentDataSet_Contingency", SegmentReportManager.BuildDataSet(this.Profile).Contingency);
                        ReportDataSource d4 = new ReportDataSource("SegmentDataSet_DiveInfoOC", SegmentReportManager.BuildDataSet(this.Profile).DiveInfoOC);
                        ReportDataSource d3 = new ReportDataSource("SegmentDataSet_DiveSegmentOC", SegmentReportManager.BuildDataSet(this.Profile).DiveSegmentOC);

                        rv.LocalReport.DataSources.Add(d);
                        rv.LocalReport.DataSources.Add(d1);
                        rv.LocalReport.DataSources.Add(d2);
                        rv.LocalReport.DataSources.Add(d3);
                        rv.LocalReport.DataSources.Add(d4);

                        List<ReportParameter> parameters = new List<ReportParameter>();

                        parameters.Add(new ReportParameter("ShowStopTime", Prefs.Current.ReportShowStopTime.ToString()));

                        bool showBail = this.Profile.HasCCRSegment;

                        if (!Prefs.Current.ReportShowBailout) showBail = false;

                        parameters.Add(new ReportParameter("ShowBailout", showBail.ToString()));

                        this.rv.LocalReport.SetParameters(parameters);


                        rv.RefreshReport();
                    }

                    break;
                case ProfileStatus.ProcessingError:
                    break;
                case ProfileStatus.InfiniteDeco:
                    break;
                case ProfileStatus.NothingToProcess:
                    break;
                case ProfileStatus.CeilingViolation:
                    break;
            }
        }
    }
}
